# coding:utf-8
from django.contrib import admin
from hostinfo.models import Host
from hostinfo.models import HostGroup
from hostinfo.models import aliyun_ecs_info
from django.http import StreamingHttpResponse
import xlwt
import os
# Register your models here.
class HostAdmin(admin.ModelAdmin):
    list_display = [
         'hostname',
         'ip',
         'osversion',
         'memory',
         'disk',
         'vendor_id',
         'model_name',
         'cpu_core',
         'product',
         'Manufacturer',
         'sn']
    actions = ["SaveExecl",]
    list_per_page = 100
    def SaveExecl(self,request,queryset):
        Begin = xlwt.Workbook(encoding = 'utf-8')
        sheet = Begin.add_sheet("hostinfo")
        style = xlwt.XFStyle() # 初始化样式
        font = xlwt.Font() # 为样式创建字体
        font.name = 'Times New Roman' 
        font.bold = True # 黑体
        style.font = font
        sheet.write(0,0,'HostName',style)
        sheet.write(0,1,'IP',style)
        sheet.write(0,2,'OsVersion',style)
        sheet.write(0,3,'memory',style)
        sheet.write(0,4,'disk',style)
        sheet.write(0,5,'vendor_id',style)
        sheet.write(0,6,'cpu_model',style)
        sheet.write(0,7,'cpu_core',style)
        sheet.write(0,8,'product',style)
        sheet.write(0,9,'Manufacturer',style)
        sheet.write(0,10,'sn',style)
        cols = 1
        for query in queryset:
            sheet.write(cols,0,unicode(query.hostname))
            sheet.write(cols,1,unicode(query.ip))
            sheet.write(cols,2,unicode(query.osversion))
            sheet.write(cols,3,unicode(query.memory))
            sheet.write(cols,4,unicode(query.disk))
            sheet.write(cols,5,unicode(query.vendor_id))
            sheet.write(cols,6,unicode(query.model_name))
            sheet.write(cols,7,unicode(query.cpu_core))
            sheet.write(cols,8,unicode(query.product))
            sheet.write(cols,9,unicode(query.Manufacturer))
            sheet.write(cols,10,unicode(query.sn))
            cols += 1
        Begin.save('{}'.format("hostinfo.xls"))
        def file_iterator(filename, chunk_size=512):
            with open(filename,'rb') as f:
                while True:
                    c = f.read(chunk_size)
                    if c:
                        yield c
                    else:
                        break
        response = StreamingHttpResponse(file_iterator('{}'.format("hostinfo.xls")))
        response['Content-Type'] = 'application/octet-stream'
        response['Content-Disposition'] = 'attachment;filename="{}"'.format("hostinfo.xls")
        return response
    SaveExecl.short_description = "导出Excel格式文件"
class HostGroupAdmin(admin.ModelAdmin):
    list_display = ['groupname']
class AliyunEcsAdmin(admin.ModelAdmin):
    list_display = [
        'instanceid',
        'zoneid',
        'instancename',
        'osname',
        'publicipaddress',
        'privateipaddress',
        'bandwidthout',
        'status',
        'creationtime',
        'expiredtime']
    actions = ["SaveExecl",]
    list_per_page = 100
    def SaveExecl(self,request,queryset):
        filename = '/tmp/aliyunSystemToExcel.xlsx'
        def file_iterator(filename, chunk_size=512):
            with open(filename,'rb') as f:
                while True:
                    c = f.read(chunk_size)
                    if c:
                        yield c
                    else:
                        break
        response = StreamingHttpResponse(file_iterator(filename))
        response['Content-Type'] = 'application/octet-stream'
        response['Content-Disposition'] = 'attachment;filename="{}"'.format("ecs_product_info.xlsx")
        return response
    SaveExecl.short_description = "导出Excel格式样式批量导入监控项"
admin.site.register(Host,HostAdmin)
admin.site.register(HostGroup,HostGroupAdmin)
admin.site.register(aliyun_ecs_info,AliyunEcsAdmin)
